﻿#region

using System;
using System.Diagnostics;
using System.Drawing;

#endregion

namespace RGG.sharp
{
    /// <summary>
    ///     生产GeometricBody的子类
    ///     工厂模式
    /// </summary>
    public class GeometricBodyFactory
    {
        /// <summary>
        ///     初始化工厂类
        /// </summary>
        /// <param name="largeDotMatrix">
        ///     需要被绘图的点阵，维度分别为LargeWidth，LargeHeight，2的矩阵
        ///     前两位为点的坐标，后一位为点存在与颜色值
        ///     即LargeDotMatrix[width,height,0] = 0表示点不存在，1表示存在
        ///     LargeDotMatrix[width,height,1] = colorValue;
        /// </param>
        public GeometricBodyFactory(int[,,] largeDotMatrix)
        {
            Debug.Assert(
                largeDotMatrix.GetLength(0) > 0 && largeDotMatrix.GetLength(1) > 0 && largeDotMatrix.GetLength(2) == 2,
                "largeDotMatrix参数设置错误");
            LargeDotMatrix = largeDotMatrix;
            ConfigGb = GeometricBodyConfig.GetInstance();
        }

        public int[,,] LargeDotMatrix { get;private set; }

        /// <summary>
        /// 点是否存在
        /// </summary>
        /// <param name="i"></param>
        /// <param name="j"></param>
        /// <returns></returns>
        public bool IsExist(int i, int j)
        {
            return LargeDotMatrix[i, j, 0] != 0;
        }

        /// <summary>
        /// 为DotMatrix设置点以及颜色
        /// </summary>
        /// <param name="i">横坐标值</param>
        /// <param name="j">纵坐标值</param>
        /// <param name="value"></param>
        /// <param name="color">颜色值</param>
        public void SetDot(int i, int j, int value, Color color)
        {
            SetDot(i,j,value,color.ToArgb());
        }

        /// <summary>
        /// 为DotMatrix设置点以及颜色
        /// </summary>
        /// <param name="i">横坐标值</param>
        /// <param name="j">纵坐标值</param>
        /// <param name="value"></param>
        /// <param name="argb">颜色值</param>
        public void SetDot(int i, int j, int value, int argb)
        {
            Debug.Assert(LargeDotMatrix[i, j, 0] == 0 || value==0, "要赋值的点原来存在有点");
            LargeDotMatrix[i, j, 0] = value == 0 ? 0 : 1;
            LargeDotMatrix[i, j, 1] = argb;
        }

        /// <summary>
        /// 获取指定点颜色
        /// </summary>
        /// <param name="i"></param>
        /// <param name="j"></param>
        /// <returns>如果点存在，则返回颜色，否则返回Color.Empty</returns>
        public Color GetColor(int i, int j)
        {
            Color color = Color.Empty;
            if (IsExist(i, j))
            {
                color = Color.FromArgb(LargeDotMatrix[i,j,1]);
            }
            return color;
        }

        public GeometricBodyConfig ConfigGb { get; private set; }

        public GeometricBody GetSharp(int i)
        {
            i = i%ConfigGb.Items.Count;
            RussianGridGameConfig config = RussianGridGameConfig.GetInstance();
            if(LargeDotMatrix==null) LargeDotMatrix = new int[config.Width, config.Height, 2];
            GeometricBody gb = new GeometricBody(ConfigGb.Items[i], LargeDotMatrix);
            Random random = new Random((int) (DateTime.Now.Ticks%int.MaxValue));
             int reverseCount = random.Next(0, 4);
            while (0 != reverseCount--)
            {
                //gb.Rotate();
            }
            return gb;
        }
    }
}